package tools;

import java.util.ArrayList;
import java.util.Random;

public class Deck
{
	public ArrayList<Card> deck = new ArrayList<Card>();
	private ArrayList<Card> trash = new ArrayList<Card>();
	private Random random = new Random();

	public Deck()
	{
		deck.add(new Card("s", "A", 1));
		deck.add(new Card("s", "2", 2));
		deck.add(new Card("s", "3", 3));
		deck.add(new Card("s", "4", 4));
		deck.add(new Card("s", "5", 5));
		deck.add(new Card("s", "6", 6));
		deck.add(new Card("s", "7", 7));
		deck.add(new Card("s", "8", 8));
		deck.add(new Card("s", "9", 9));
		deck.add(new Card("s", "10", 10));
		deck.add(new Card("s", "J", 11));
		deck.add(new Card("s", "Q", 12));
		deck.add(new Card("s", "K", 13));

		deck.add(new Card("h", "A", 1));
		deck.add(new Card("h", "2", 2));
		deck.add(new Card("h", "3", 3));
		deck.add(new Card("h", "4", 4));
		deck.add(new Card("h", "5", 5));
		deck.add(new Card("h", "6", 6));
		deck.add(new Card("h", "7", 7));
		deck.add(new Card("h", "8", 8));
		deck.add(new Card("h", "9", 9));
		deck.add(new Card("h", "10", 10));
		deck.add(new Card("h", "J", 11));
		deck.add(new Card("h", "Q", 12));
		deck.add(new Card("h", "K", 13));

		deck.add(new Card("d", "A", 1));
		deck.add(new Card("d", "2", 2));
		deck.add(new Card("d", "3", 3));
		deck.add(new Card("d", "4", 4));
		deck.add(new Card("d", "5", 5));
		deck.add(new Card("d", "6", 6));
		deck.add(new Card("d", "7", 7));
		deck.add(new Card("d", "8", 8));
		deck.add(new Card("d", "9", 9));
		deck.add(new Card("d", "10", 10));
		deck.add(new Card("d", "J", 11));
		deck.add(new Card("d", "Q", 12));
		deck.add(new Card("d", "K", 13));

		deck.add(new Card("c", "A", 1));
		deck.add(new Card("c", "2", 2));
		deck.add(new Card("c", "3", 3));
		deck.add(new Card("c", "4", 4));
		deck.add(new Card("c", "5", 5));
		deck.add(new Card("c", "6", 6));
		deck.add(new Card("c", "7", 7));
		deck.add(new Card("c", "8", 8));
		deck.add(new Card("c", "9", 9));
		deck.add(new Card("c", "10", 10));
		deck.add(new Card("c", "J", 11));
		deck.add(new Card("c", "Q", 12));
		deck.add(new Card("c", "K", 13));

	}
	
	/* karty wydane graczom */
	public Card getCard()
	{
		if (deck.isEmpty()) 
			trashToDeck();

		int index;
		if (deck.size()>1)
			index = random.nextInt(deck.size() - 1);
		else
			index=0;
		
		Card c = deck.get(index);
		deck.remove(index);
		
		return c;
	}

	/* zwrot wszystkich kart po rozgrywce */
	public void returnCards(ArrayList<Card> cards)
	{
		for(Card c : cards)
			this.putToTrash(c);
	}
	
	/* wkladamy wymienione karty graczy do trash */
	public void putToTrash(Card card)
	{
		trash.add(card);
	}
	
	/* podmiana trasha z deckiem w przypadku skonczenia sie talii */
	private void trashToDeck() 
	{
		for(Card c : trash)
			deck.add(c);
		trash.clear();
	}
}
